home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / yacas_alg / yacas_morphos / share / yacas / include / lispenvironment.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  9.8 KB  |  403 lines

  1. /** \file lispenvironment.h
  2.  *  General environment access.
  3.  *
  4.  */
  5.  
  6.  
  7. #ifndef __lispenvironment_h__
  8. #define __lispenvironment_h__
  9.  
  10. #include "yacasbase.h"
  11. #include "lispobject.h"
  12. #include "lisphash.h"
  13. #include "lispevalhash.h"
  14. #include "lispcleanupstack.h"
  15. #include "deffile.h"
  16. #include "ramdisk.h"
  17. #include "lispio.h"
  18. #include "stringio.h"
  19. #include "lispglobals.h"
  20. #include "lispplugin.h"
  21. #include "ctokenizer.h"
  22. #include "xmltokenizer.h"
  23.  
  24.  
  25. class LispDefFiles;
  26. class InputDirectories : public CDeletingArrayGrower<LispStringPtr>
  27. {
  28. };
  29.  
  30. class LispInput;
  31. class LispOutput;
  32. class LispPrinter;
  33. class LispOperators;
  34. class LispUserFunctions;
  35. class LispUserFunction;
  36. class LispMultiUserFunction;
  37. class LispEvaluatorBase;
  38. class BasicEvaluator;
  39. class LispDllBase;
  40. class YacasDebuggerBase;
  41. class LispEnvironment;
  42. class CDllArray : public CDeletingArrayGrower<LispDllBase*>
  43. {
  44. public:
  45.     void DeleteNamed(LispCharPtr aName, LispEnvironment& aEnvironment);
  46. };
  47.  
  48.  
  49. class LispEnvironment : public YacasBase
  50. {
  51. public:
  52.     LispEnvironment(LispCommands& aCommands,
  53.                     LispUserFunctions& aUserFunctions,
  54.                     LispGlobal& aGlobals,
  55.                     LispHashTable& aHashTable,
  56.                     LispOutput* aOutput,
  57.                     LispPrinter& aPrinter,
  58.                     LispOperators &aPreFixOperators,
  59.                     LispOperators &aInFixOperators,
  60.                     LispOperators &aPostFixOperators,
  61.                     LispOperators &aBodiedOperators,
  62.                     LispInput*    aCurrentInput);
  63.     ~LispEnvironment();
  64. public:
  65.     void SetVariable(LispStringPtr aString, LispPtr& aValue);
  66.     void GetVariable(LispStringPtr aVariable,LispPtr& aResult);
  67.     void SetGlobalEvaluates(LispStringPtr aVariable);
  68.  
  69.     void UnsetVariable(LispStringPtr aString);
  70.     void PushLocalFrame(LispBoolean aFenced);
  71.     void PopLocalFrame();
  72.     void NewLocal(LispStringPtr aVariable,LispObject* aValue);
  73. public:
  74.     inline LispCommands& Commands();
  75.     void SetCommand(LispEvalCaller aEvaluatorFunc, LispCharPtr aString);
  76.     void RemoveCommand(LispCharPtr aString);
  77.  
  78.     inline  LispHashTable& HashTable();
  79.     LispUserFunction* UserFunction(LispPtr& aArguments);
  80.     LispUserFunction* UserFunction(LispStringPtr aName,LispInt aArity);
  81.     LispMultiUserFunction* MultiUserFunction(LispStringPtr aArguments);
  82.     LispDefFiles& DefFiles();
  83.     void DeclareRuleBase(LispStringPtr aOperator, LispPtr& aParameters);
  84.     void DefineRule(LispStringPtr aOperator,LispInt aArity,
  85.                             LispInt aPrecedence, LispPtr& aPredicate,
  86.                             LispPtr& aBody);
  87.     void DefineRulePattern(LispStringPtr aOperator,LispInt aArity,
  88.                             LispInt aPrecedence, LispPtr& aPredicate,
  89.                             LispPtr& aBody);
  90.     void UnFenceRule(LispStringPtr aOperator,LispInt aArity);
  91.     void Retract(LispStringPtr aOperator,LispInt aArity);
  92.     void HoldArgument(LispStringPtr  aOperator,LispStringPtr aVariable);
  93.  
  94. public:
  95.     inline void SetPrecision(LispInt aPrecision);
  96.     inline LispInt Precision(void);
  97. public:
  98.     inline void SetPrettyPrinter(LispStringPtr aPrettyPrinter);
  99.     inline LispStringPtr PrettyPrinter(void);
  100. public:
  101.     LispInt GetUniqueId();
  102. public:
  103.     LispPrinter& CurrentPrinter();
  104. public:
  105.     LispOperators& PreFix();
  106.     LispOperators& InFix();
  107.     LispOperators& PostFix();
  108.     LispOperators& Bodied();
  109. public:
  110.     LispInput* CurrentInput();
  111.     void SetCurrentInput(LispInput* aInput);
  112. public:
  113.     LispOutput* CurrentOutput();
  114.     void SetCurrentOutput(LispOutput* aOutput);
  115. public:
  116.     void SetUserError(LispCharPtr aErrorString);
  117.     LispCharPtr ErrorString(LispInt aError);
  118.     
  119. protected:
  120.     LispInt iPrecision;
  121. public:
  122.     InputDirectories iInputDirectories;
  123.     DeletingLispCleanup iCleanup;
  124.     LispInt iEvalDepth;
  125.     LispInt iMaxEvalDepth;
  126.     LispRamDisk iRamDisk;
  127.     LispEvaluatorBase* iEvaluator;
  128.  
  129. public: // Error information when some error occurs.
  130.     InputStatus iInputStatus;
  131.     LispInt iSecure;
  132. public: // pre-found
  133.     LispStringPtr iTrue;
  134.     LispStringPtr iFalse;
  135.  
  136.     LispStringPtr iEndOfFile;
  137.     LispStringPtr iEndStatement;
  138.     LispStringPtr iProgOpen;
  139.     LispStringPtr iProgClose;
  140.     LispStringPtr iNth;
  141.     LispStringPtr iBracketOpen;
  142.     LispStringPtr iBracketClose;
  143.     LispStringPtr iListOpen;
  144.     LispStringPtr iListClose;
  145.     LispStringPtr iComma;
  146.     LispStringPtr iList;
  147.     LispStringPtr iProg;
  148.  
  149.     LispPtr iTrueAtom;
  150.     LispPtr iFalseAtom;
  151.     LispInt iLastUniqueId;
  152.  
  153. public: // Error reporting
  154.     LispString iError;
  155.     StringOutput iErrorOutput;
  156.     CDllArray iDlls;
  157.     YacasDebuggerBase* iDebugger;
  158.     
  159. private:
  160.     LispPtr *FindLocal(LispStringPtr aVariable);
  161.  
  162. private:
  163.  
  164.     class LispLocalVariable : public YacasBase
  165.     {
  166.     public:
  167.         LispLocalVariable(LispStringPtr aVariable,
  168.                           LispObject* aValue)
  169.             : iNext(NULL), iVariable(aVariable),iValue(*aValue)
  170.         {
  171.             aVariable->IncreaseRefCount();
  172.         };
  173.         ~LispLocalVariable()
  174.         {
  175.             iVariable->DecreaseRefCount();
  176.         }
  177.     public:
  178.         LispLocalVariable* iNext;
  179.         LispStringPtr iVariable;
  180.         LispPtr iValue;
  181.     };
  182.     class LocalVariableFrame : public YacasBase
  183.     {
  184.     public:
  185.         LocalVariableFrame(LocalVariableFrame *aNext,
  186.                            LispLocalVariable* aFirst)
  187.             : iNext(aNext), iFirst(aFirst), iLast(aFirst) { }
  188.         void Add(LispLocalVariable* aNew)
  189.         {
  190.             aNew->iNext = iFirst;
  191.             iFirst = aNew;
  192.         }
  193.         ~LocalVariableFrame()
  194.         {
  195.             LispLocalVariable* t = iFirst;
  196.             LispLocalVariable* next;
  197.             while (t != iLast)
  198.             {
  199.                 next = t->iNext;
  200.                 delete t;
  201.                 t = next;
  202.             }
  203.         }
  204.         
  205.     public:
  206.         LocalVariableFrame *iNext;
  207.         LispLocalVariable* iFirst;
  208.         LispLocalVariable* iLast;
  209.     };
  210. public: //Well... only because I want to be able to show the stack to the outside world...
  211.     LocalVariableFrame *iLocalsList;
  212. private:
  213.     LispCommands&  iCommands;
  214.     LispUserFunctions& iUserFunctions;
  215.     LispHashTable& iHashTable;
  216.     LispDefFiles   iDefFiles;
  217.     LispPrinter&   iPrinter;
  218.     LispOutput*    iCurrentOutput;
  219.  
  220.     LispGlobal&    iGlobals;
  221.  
  222. //    LispPtr        iLocals;
  223.  
  224.     LispOperators& iPreFixOperators;
  225.     LispOperators& iInFixOperators;
  226.     LispOperators& iPostFixOperators;
  227.     LispOperators& iBodiedOperators;
  228.  
  229.     LispInput* iCurrentInput;
  230.  
  231.     LispCharPtr theUserError;
  232.  
  233.     LispStringPtr iPrettyPrinter;
  234. public:
  235.     LispTokenizer iDefaultTokenizer;
  236.     CTokenizer    iCTokenizer;
  237.     XmlTokenizer  iXmlTokenizer;
  238.     LispTokenizer* iCurrentTokenizer;
  239. };
  240.  
  241.  
  242. inline void LispEnvironment::SetPrecision(LispInt aPrecision)
  243. {
  244.     iPrecision = aPrecision;
  245. }
  246.  
  247. inline LispInt LispEnvironment::Precision(void)
  248. {
  249.     return iPrecision;
  250. }
  251.  
  252. inline LispCommands& LispEnvironment::Commands()
  253. {
  254.     return iCommands;
  255. }
  256.  
  257. inline LispHashTable& LispEnvironment::HashTable()
  258. {
  259.     return iHashTable;
  260. }
  261.  
  262.  
  263.  
  264. // Local lisp stack, unwindable by the exception handler
  265. class LispLocalFrame : public LispBase
  266. {
  267. public:
  268.     LispLocalFrame(LispEnvironment& aEnvironment, LispBoolean aFenced)
  269.         : iEnvironment(aEnvironment)
  270.     {
  271.         iEnvironment.PushLocalFrame(aFenced);
  272.         SAFEPUSH(iEnvironment,*this);
  273.     };
  274.     virtual ~LispLocalFrame()
  275.     {
  276.         SAFEPOP(iEnvironment);
  277.         Delete();
  278.     };
  279.     virtual void Delete();
  280. private:
  281.     LispEnvironment& iEnvironment;
  282. };
  283.  
  284.  
  285.  
  286. class LispSecureFrame : public LispBase
  287. {
  288. public:
  289.     LispSecureFrame(LispEnvironment& aEnvironment)
  290.         : iEnvironment(aEnvironment)
  291.     {
  292.         iPreviousSecure = iEnvironment.iSecure;
  293.         iEnvironment.iSecure = 1;
  294.         SAFEPUSH(iEnvironment,*this);
  295.     };
  296.     virtual ~LispSecureFrame()
  297.     {
  298.         SAFEPOP(iEnvironment);
  299.         Delete();
  300.     };
  301.     virtual void Delete();
  302. private:
  303.     LispEnvironment& iEnvironment;
  304.     LispInt iPreviousSecure;
  305. };
  306.  
  307.  
  308. // LispLocalInput takes ownership over the LispInput class
  309. class LispLocalInput : public LispBase
  310. {
  311. public:
  312.     LispLocalInput(LispEnvironment& aEnvironment, LispInput* aInput)
  313.         : iEnvironment(aEnvironment)
  314.     {
  315.         iPreviousInput = iEnvironment.CurrentInput();
  316.         iEnvironment.SetCurrentInput(aInput);
  317.         SAFEPUSH(iEnvironment,*this);
  318.     };
  319.     virtual ~LispLocalInput()
  320.     {
  321.         SAFEPOP(iEnvironment);
  322.         Delete();
  323.     };
  324.     virtual void Delete();
  325. private:
  326.     LispEnvironment& iEnvironment;
  327.     LispInput* iPreviousInput;
  328. };
  329.  
  330.  
  331. // LispLocalInput takes ownership over the LispInput class
  332. class LispLocalOutput : public LispBase
  333. {
  334. public:
  335.     LispLocalOutput(LispEnvironment& aEnvironment, LispOutput* aOutput)
  336.         : iEnvironment(aEnvironment)
  337.     {
  338.         iPreviousOutput = iEnvironment.CurrentOutput();
  339.         iEnvironment.SetCurrentOutput(aOutput);
  340.         SAFEPUSH(iEnvironment,*this);
  341.     };
  342.     virtual ~LispLocalOutput()
  343.     {
  344.         SAFEPOP(iEnvironment);
  345.         Delete();
  346.     };
  347.     virtual void Delete();
  348. private:
  349.     LispEnvironment& iEnvironment;
  350.     LispOutput* iPreviousOutput;
  351. };
  352.  
  353.  
  354. class LispLocalEvaluator : public YacasBase
  355. {
  356. public:
  357.     LispLocalEvaluator(LispEnvironment& aEnvironment,LispEvaluatorBase* aNewEvaluator);
  358.     ~LispLocalEvaluator();
  359.     
  360. private:
  361.     LispEvaluatorBase* iPreviousEvaluator;
  362.     LispEnvironment& iEnvironment;
  363. };
  364.  
  365. class LispLocalTrace : public YacasBase
  366. {
  367. public:
  368.     LispLocalTrace(LispUserFunction* aUserFunc);
  369.     ~LispLocalTrace();
  370. private:
  371.     LispUserFunction* iUserFunc;
  372. };
  373.  
  374. class LocalArgs : public YacasBase
  375. {
  376. public:
  377.     LocalArgs(LispPtr* aPtrs)
  378.         : iPtrs(aPtrs)
  379.     {};
  380.     ~LocalArgs()
  381.     {
  382.         if (iPtrs)
  383.             delete[] iPtrs;
  384.     }
  385. private:
  386.     LispPtr* iPtrs;
  387. };
  388.  
  389.  
  390.  
  391. inline void LispEnvironment::SetPrettyPrinter(LispStringPtr aPrettyPrinter)
  392. {
  393.     iPrettyPrinter = aPrettyPrinter;
  394. }
  395. inline LispStringPtr LispEnvironment::PrettyPrinter(void)
  396. {
  397.     return iPrettyPrinter;
  398. }
  399.  
  400.  
  401. #endif
  402.  
  403.